package com.gym.code.config.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gym.code.config.admin.bean.ColumnMappingBean;
import com.gym.code.config.admin.bean.method.MethodMappingBean;
import com.gym.code.config.admin.bean.table.TableBaseMappingBean;
import com.gym.code.config.admin.bean.table.TableMappingBean;
import com.gym.code.config.admin.dao.IColumnMappingDao;
import com.gym.code.config.admin.dao.ITableMappingDao;
import com.gym.code.config.admin.service.IMethodMappingService;
import com.gym.code.config.admin.service.ITableMappingService;

/**
 * 
 * @author xinchun.wang
 * @email: 532002108@qq.com
 * @createTime 2015-4-11 下午4:39:36
 */
@Service
public class TableMappingService implements ITableMappingService{

	@Autowired
	private ITableMappingDao tableMappingDao;
	
	@Autowired
	private IColumnMappingDao   columnMappingDao;
	
	@Autowired
	private IMethodMappingService methodMappingService;
	
	@Override
	public int insert(TableBaseMappingBean bean) {
		return tableMappingDao.insert(bean);
	}

	@Override
	public TableBaseMappingBean queryBase(long id) {
		return tableMappingDao.query(id);
	}

	@Override
	public int update(TableBaseMappingBean bean) {
		return tableMappingDao.update(bean);
	}

	@Override
	public TableMappingBean query(long id) {
		TableBaseMappingBean baseBean = queryBase(id);
		return wrapTableMappingBean(baseBean);
	}
	
	
	private TableMappingBean wrapTableMappingBean(TableBaseMappingBean baseBean){
		TableMappingBean bean = new TableMappingBean(baseBean);
		List<ColumnMappingBean> columnListe = columnMappingDao.queryByTableId(baseBean.getId());
		bean.getColumnList().addAll(columnListe);
		
		List<MethodMappingBean> methodList = methodMappingService.queryByTableId(baseBean.getId());
		bean.getMethodList().addAll(methodList);
		return bean;
	}

	@Override
	public List<TableMappingBean> queryAll(long dbId) {
		List<TableBaseMappingBean> baseResult = queryBaseAll(dbId);
		
		List<TableMappingBean> result = new ArrayList<TableMappingBean>();
		for(TableBaseMappingBean item: baseResult){
			result.add(wrapTableMappingBean(item));
		}
		return result;
	}

	@Override
	public List<TableBaseMappingBean> queryBaseAll(long dbId) {
		return tableMappingDao.queryBaseAll(dbId);
	}
	
}
